package com.woniuxy02;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 作者
 * @create 2025-11-10 14:06
 */
public class Test01 {
    public static void main(String[] args) {
        test08();

    }

    private static void test08() {
        HashMap<Student,String> map=new HashMap<>();
        map.put(new Student(1,"zs",11),"zs");
        map.put(new Student(2,"ls",12),"ls");
        map.put(new Student(3,"ww",13),"ww");
//        Set<Student> keys=map.keySet();
//        for(Student key : keys){
//            System.out.println(key);
//            System.out.println(map.get(key));
//            System.out.println("----------------------");
//        }

//        Collection<String> values = map.values();
//        for( String value:values){
//            System.out.println(value);
//        }

//        Set<Map.Entry<Student,String>> entries = map.entrySet();
//        for(Map.Entry<Student,String>  entry : entries){
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
//        }
    }

    private static void test07() {
        HashMap<String,Student> map=new HashMap<>();
        map.put("s001", new Student(1,"zs",11));
        map.put("s002", new Student(2,"ls",12));
        map.put("s003", new Student(3,"ww",13));
        //所有key的集合      Set<key>
        //所有value的集合    Collection<value>
        //entrySet()方法，获取所有键值对的集合  Set
        //Entry<K,V> 就是键值对，定义在Map接口中的
        Set<Map.Entry<String, Student>> entries = map.entrySet();
        for(Map.Entry<String, Student>  entry : entries){
            System.out.println("entry:"+entry);
            System.out.println("key:"+entry.getKey());
            System.out.println("value:"+entry.getValue());
            System.out.println("-----------------");
        }
    }

    private static void test06() {
        HashMap<String,Student> map=new HashMap<>();
        map.put("s001", new Student(1,"zs",11));
        map.put("s002", new Student(2,"ls",12));
        map.put("s003", new Student(3,"ww",13));
        Collection<Student> values = map.values();
        for(Student student : values){
            System.out.println(student);
        }
    }

    private static void test05() {
        HashMap<String,Student> map=new HashMap<>();
        map.put("s001", new Student(1,"zs",11));
        map.put("s002", new Student(2,"ls",12));
        map.put("s003", new Student(3,"ww",13));
        //获取map中所有key的集合
        Set<String> keys= map.keySet();
        for(String key :keys){
            System.out.println(key);
            System.out.println(map.get(key));
            System.out.println("-----------------------");
        }
    }

    private static void test04() {
        HashMap<Integer,String> map=new HashMap<>();
        //添加元素，指定key,value
        map.put(1,"zs");
        map.put(2,"ls");
        map.put(3,"ww");
        System.out.println(map.containsKey(1));
        System.out.println(map.containsKey(5));
        System.out.println(map.containsValue("zs"));
        System.out.println(map.containsValue("zl"));
        System.out.println(map);
    }

    private static void test03() {
        HashMap<String,String> map=new HashMap<>();
        map.put("a", "zs");
        map.put("b", "ls");
        map.put("c", "ww");
        System.out.println(map.get("a"));
        System.out.println(map.get("b"));
        System.out.println(map.get("c"));
        System.out.println(map.get("d"));
    }

    private static void test01() {
        //使用泛型，同时指定key和value的类
        //key和value可以是任何类型，
        // 一般key使用简单的类型(Integer,String),value存储核心数据(String,对象)
        HashMap<Integer,String> map=new HashMap<>();
        //添加元素，指定key,value
        map.put(1,"aaa");
        map.put(2,"bbb");
        map.put(3,"aaa");
//        map.put(3,"xxx");
        //访问元素，通过key获取value
        //key不允许重复，添加了相同的key，后面的value值覆盖之前的值
        System.out.println(map.get(1));
        System.out.println(map.get(2));
        System.out.println(map.get(3));
    }
}
